#include	"a_global_vars.h"

void compute_performance(int sd, short event, void* arg) {
	if (rtag) {
		gettimeofday(&performance_end_time, NULL);
		gettimeofday(&endTime, NULL);

		a_interval_performance->interval_time = measure_time_difference(performance_end_time, performance_start_time);
		a_interval_performance->total_time = measure_time_difference(endTime, startTime);
		a_interval_performance->interval_sending_throughput = calculate_throughput((a_interval_performance->interval_bytes_sent), a_interval_performance->interval_time);
		a_interval_performance->interval_delivery_throughput = calculate_throughput(a_interval_performance->interval_num_packets_delivered * (sizeof(proposer_msg) - PROPOSER_MSG_HEADER), a_interval_performance->interval_time);
		a_interval_performance->interval_recv_throughput = calculate_throughput(a_interval_performance->interval_bytes_received, a_interval_performance->interval_time);
		a_interval_performance->interval_latency = a_interval_performance->interval_latency / (double) (a_interval_performance->interval_num_packets_delivered * MAX_MSG_COUNT);
		a_interval_performance->total_num_packets_delivered += a_interval_performance->interval_num_packets_delivered;

		a_calculate_performance_over_all_interval_uptonow();
		a_store_performance_in_array(&a_performances[0], &a_next_performance_index, a_interval_performance);
		a_print_performance(a_interval_performance);
		a_nulify_performance(a_interval_performance);

		if (a_next_performance_index >= STOPPING_CONDITION) {
			a_store_performance_in_file(a_performance_file, a_next_performance_index);
			free(data);
			sleep(4);
			exit(1);
		}
	}
	gettimeofday(&performance_start_time, NULL);
	int status = evtimer_add(print_stat_timeout_eve, &tv2);
	assert(status >= 0);
}
double measure_time_difference(struct timeval now, struct timeval past) {
	struct timeval elapsed_interval;

	elapsed_interval.tv_sec = now.tv_sec - past.tv_sec;
	elapsed_interval.tv_usec = (now.tv_usec > past.tv_usec ? now.tv_usec - past.tv_usec : past.tv_usec - now.tv_usec);
	double msec_interval = 1000 * elapsed_interval.tv_sec + elapsed_interval.tv_usec / (double) 1000;

	return msec_interval;
}
double calculate_throughput(int bytes, double elapsed_time) {
	double thr = (bytes / (double) (1024 * 1024));
	thr = thr / (double) elapsed_time;
	thr = thr * 1000 * 8;
	return thr;
}
void a_calculate_performance_over_all_interval_uptonow() {
	int i;
	double avg_t = 0, avg_s_t = 0, avg_l = 0;

	for (i = 0; i < a_next_performance_index; i++) {
		avg_s_t += a_performances[i].interval_sending_throughput;
		avg_t += a_performances[i].interval_delivery_throughput;
		avg_l += a_performances[i].interval_latency;
	}
	a_interval_performance->total_sending_throughput = (avg_s_t + a_interval_performance->interval_delivery_throughput) / (double) (a_next_performance_index + 1);
	a_interval_performance->total_delivery_throughput = (avg_t + a_interval_performance->interval_delivery_throughput) / (double) (a_next_performance_index + 1);
	a_interval_performance->total_latency = (avg_l + a_interval_performance->interval_latency) / (double) (a_next_performance_index + 1);
}
void a_store_performance_in_array(a_performance * a_performances_array, int * performance_index, a_performance *i_p) {
	memcpy((a_performances_array + *performance_index), i_p, sizeof(a_performance));
	*(performance_index) = *(performance_index) + 1;
}

void a_print_performance(a_performance *p) {
	printf("------------------------------------------------------------------------------------------------------------------\n");
	printf("TOTAL_TIME:   %f INTERVAL_TIME: %f  TICKS_PER_SECOND: %f\n\n", p->total_time, p->interval_time, p->interval_num_entrance);
	printf("INTERVAL DELIVERY: NUM_PACKETS: %d, THROUGHPUT: %f\n", p->interval_num_packets_delivered, p->interval_delivery_throughput);
	printf("INTERVAL SENT: 	   NUM_PACKETS: %d, THROUGHPUT: %f\n", p->interval_num_packets_sent, p->interval_sending_throughput);
	printf("INTERVAL RECV: 	   NUM_PACKETS: %d, THROUGHPUT: %f\n\n", p->interval_num_packets_proposer_to_acceptor_0, p->interval_recv_throughput);
	printf("TOTAL DELIVERY:    NUM_PACKETS: %d, THROUGHPUT: %f\n", p->total_num_packets_delivered, p->total_delivery_throughput);
	printf("TOTAL SENT:        NUM_PACKETS: %d, THROUGHPUT: %f\n\n", p->total_num_packets_sent, p->total_sending_throughput);
	printf("INTERVAL REDUNDANT PACKETS:     %d  LOST_PACKETS: %d\n", p->interval_num_redundant_packets_received, p->interval_num_packets_lost);
	printf("TOTAL    REDUNDANT PACKETS:     %d\n\n", p->total_num_redundant_packets_received);
	printf("LATENCY: %f\n\n", p->interval_latency);
}

void a_store_performance_in_file(const char * performance_file, int num_entries) {

	FILE *performance_fp;
	int i;

	performance_fp = fopen(performance_file, "w");

	assert(performance_fp != NULL);
	for (i = 0; i < num_entries; i++) {
		//1 2  3  4   5  6  7  8  9 10 11 12 13 14 15 16 17
		fprintf(performance_fp, "%d %d %d %f %f %d %f %d %f %d %f %d %f %d %d %f %d %f %d\n", i + 1, //1 - %d
				SUBMIT_PER_WAKE, //2-  %d
				(int) tv1.tv_usec, //3 - %d
				a_performances[i].total_time, //4 - %d
				a_performances[i].interval_time, //5 - %d
				a_performances[i].interval_num_packets_delivered, //6 - %d
				a_performances[i].interval_delivery_throughput, //7 - %f
				a_performances[i].interval_num_packets_sent, //8 - %d
				a_performances[i].interval_sending_throughput, //9 - %f
				a_performances[i].total_num_packets_delivered, //10 - %d
				a_performances[i].total_delivery_throughput, //11 - %f
				a_performances[i].total_num_packets_sent, //12 - %d
				a_performances[i].total_sending_throughput, //13 - %f
				a_performances[i].total_num_redundant_packets_received, //14 - %d
				a_performances[i].interval_num_redundant_packets_received, //15 - %d
				a_performances[i].interval_num_entrance, //16 - %f
				a_performances[i].highest_deliverable_inst_id, //17 - %d
				a_performances[i].interval_latency, //18 - %f
				a_performances[i].interval_num_packets_lost); //19 - %d

	}

	fclose(performance_fp);
}
void a_nulify_performance(a_performance *p) {
	p->interval_time = 0;
	p->interval_num_packets_delivered = 0;
	p->interval_delivery_throughput = 0;
	p->interval_num_packets_sent = 0;
	p->interval_sending_throughput = 0;
	p->interval_num_redundant_packets_received = 0;
	p->interval_num_entrance = 0;
	p->interval_latency = 0;
	p->interval_num_packets_received = 0;
	p->interval_num_packets_proposer_to_acceptor_0 = 0;
	p->interval_num_packets_lost = 0;
	p->interval_num_overwritten_insts = 0;
	p->interval_bytes_sent = 0;
	p->interval_bytes_received = 0;

	if (a_next_performance_index == 0) {
		p->total_time = 0;
		p->total_num_packets_delivered = 0;
		p->total_delivery_throughput = 0;
		p->total_num_packets_sent = 0;
		p->total_sending_throughput = 0;
		p->total_num_redundant_packets_received = 0;
		p->total_num_entrance = 0;
		p->total_latency = 0;
		p->total_num_packets_received = 0;
		p->total_num_overwritten_insts = 0;

	}
}

